package com.wei.tang.fragment;

import java.util.ArrayList;
import java.util.List;

import com.wei.tang.AppConest;
import com.wei.tang.AppContext;
import com.wei.tang.R;
import com.wei.tang.adapter.HorizontalCourseAdapter;
import com.wei.tang.base.BaseFragment;
import com.wei.tang.bean.CourseBean;
import com.wei.tang.db.CourseDao;
import com.wei.tang.db.JsonData;
import com.wei.tang.ui.course.CourseAcitivity;
import com.wei.tang.util.Tlog;
import com.wei.tang.util.Util;
import com.wei.tang.widget.HorizontalListView;
import com.wei.tang.widget.banner.ABaseTransformer;
import com.wei.tang.widget.banner.AccordionTransformer;
import com.wei.tang.widget.banner.CBViewHolderCreator;
import com.wei.tang.widget.banner.ConvenientBanner;
import com.wei.tang.widget.banner.LocalImageHolderView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

public class CourseInformationFragment extends BaseFragment {
	private Context mySelf = null;

	@InjectView(R.id.rl_app_head_left)
	RelativeLayout rl_app_head_left;
	@InjectView(R.id.tv_app_head_left_content)
	TextView tv_app_head_left_content;
	@InjectView(R.id.iv_app_head_left_image)
	ImageView iv_app_head_left_image;
	@InjectView(R.id.rl_app_head_center)
	RelativeLayout rl_app_head_center;
	@InjectView(R.id.tv_app_head_center_content)
	TextView tv_app_head_center_content;
	@InjectView(R.id.iv_app_head_center_image)
	ImageView iv_app_head_center_image;
	@InjectView(R.id.rl_app_head_right)
	RelativeLayout rl_app_head_right;
	@InjectView(R.id.tv_app_head_right_content)
	TextView tv_app_head_right_content;
	@InjectView(R.id.iv_app_head_right_image)
	ImageView iv_app_head_right_image;

	@InjectView(R.id.tv_couser_fragment_rec)
	TextView tv_couser_fragment_rec;
	@InjectView(R.id.hlv_couser_fragment_rec)
	HorizontalListView hlv_couser_fragment_rec;

	@InjectView(R.id.hlv_couser_fragment_new)
	HorizontalListView hlv_couser_fragment_new;

	@InjectView(R.id.hlv_couser_fragment_free)
	HorizontalListView hlv_couser_fragment_free;

	@InjectView(R.id.hlv_couser_fragment_diagram)
	HorizontalListView hlv_couser_fragment_diagram;

	@InjectView(R.id.hlv_couser_fragment_road)
	HorizontalListView hlv_couser_fragment_road;

	@InjectView(R.id.hlv_couser_fragment_boutique)
	HorizontalListView hlv_couser_fragment_boutique;

	@InjectView(R.id.cb_couser_fragment_banner)
	ConvenientBanner cb_couser_fragment_banner;

	private ArrayList<Integer> localImages = new ArrayList<Integer>();
	private List<CourseBean> courseDatas = null;
	private List<CourseBean> newDatas = null;
	private List<CourseBean> hotDatas = null;
	private List<CourseBean> meDatas = null;
	private List<CourseBean> sqldatas = null;
	private HorizontalCourseAdapter courseAdapter = null;
	private HorizontalCourseAdapter newAdapter = null;
	private HorizontalCourseAdapter hotAdapter = null;
	private HorizontalCourseAdapter meAdapter = null;

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
			@Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.course_information_fragment, container, false);
		ButterKnife.inject(this, view);
		initData();
		initView(view);
		return view;
	}

	@Override
	public void initView(View view) {
		tv_app_head_center_content.setText("课程");
		addABaseTransformer();
	}

	@Override
	public void initData() {
		loadTestDatas();
		courseDatas = JsonData.getCourseBeans();
		newDatas = JsonData.getNewBeans();
		hotDatas = JsonData.getHotBeans();
		meDatas = JsonData.getMeBeans();
		courseAdapter = new HorizontalCourseAdapter(mySelf, courseDatas);
		newAdapter = new HorizontalCourseAdapter(mySelf, newDatas);
		hotAdapter = new HorizontalCourseAdapter(mySelf, hotDatas);
		meAdapter = new HorizontalCourseAdapter(mySelf, meDatas);
		hlv_couser_fragment_rec.setAdapter(courseAdapter);
		addItemClick(hlv_couser_fragment_rec);
		hlv_couser_fragment_new.setAdapter(newAdapter);
		addItemClick(hlv_couser_fragment_new);
		hlv_couser_fragment_free.setAdapter(hotAdapter);
		addItemClick(hlv_couser_fragment_free);
		hlv_couser_fragment_diagram.setAdapter(meAdapter);
		addItemClick(hlv_couser_fragment_diagram);
		hlv_couser_fragment_road.setAdapter(courseAdapter);
		addItemClick(hlv_couser_fragment_road);
		hlv_couser_fragment_road.setAdapter(hotAdapter);
		addItemClick(hlv_couser_fragment_road);
		hlv_couser_fragment_boutique.setAdapter(meAdapter);
		addItemClick(hlv_couser_fragment_boutique);

		cb_couser_fragment_banner.setPages(new CBViewHolderCreator<LocalImageHolderView>() {
			@Override
			public LocalImageHolderView createHolder() {
				return new LocalImageHolderView();
			}
		}, localImages);
		cb_couser_fragment_banner
				.setPageIndicator(new int[] { R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused });
		cb_couser_fragment_banner.setOnItemClickListener(new com.wei.tang.widget.banner.OnItemClickListener() {
			@Override
			public void onItemClick(int position) {
				Intent intent = new Intent(mySelf, CourseAcitivity.class);
				intent.putExtra("image", courseDatas.get(position).getCourse_size());
				intent.putExtra("path", courseDatas.get(position).getCourse_url());
				intent.putExtra("describle", courseDatas.get(position).getCourse_classifyid());
				mySelf.startActivity(intent);
			}
		});
	}

	/**
	 * 给控件添加监听
	 * 
	 * @param hl
	 */
	public void addItemClick(HorizontalListView hl) {
		hl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(mySelf, CourseAcitivity.class);
				intent.putExtra("image", courseDatas.get(position).getCourse_size());
				intent.putExtra("path", courseDatas.get(position).getCourse_url());
				intent.putExtra("describle", courseDatas.get(position).getCourse_classifyid());
				mySelf.startActivity(intent);
			}
		});
	}

	/*
	 * 加入测试Views
	 */
	private void loadTestDatas() {
		// 本地图片集合
		for (int position = 0; position < 3; position++)
			localImages.add(Util.getResId("classtitle" + position, R.drawable.class));
	}

	/**
	 * 添加3d的翻页效果
	 */
	public void addABaseTransformer() {
		String transforemerName = AccordionTransformer.class.getSimpleName();
		try {
			Class cls = Class.forName("com.wei.tang.widget.banner." + transforemerName);
			ABaseTransformer transforemer = (ABaseTransformer) cls.newInstance();
			cb_couser_fragment_banner.getViewPager().setPageTransformer(true, transforemer);
			// 部分3D特效需要调整滑动速度
			if (transforemerName.equals("StackTransformer")) {
				cb_couser_fragment_banner.setScrollDuration(1200);
			}
		} catch (ClassNotFoundException e) {
			Tlog.i("ClassNotFoundException e" + e.toString());
		} catch (java.lang.InstantiationException e) {
			Tlog.i("InstantiationException e" + e.toString());
		} catch (IllegalAccessException e) {
			Tlog.i("IllegalAccessException e" + e.toString());
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		// 开始自动翻页
		cb_couser_fragment_banner.startTurning(5000);

		if (sqldatas != null) {
			sqldatas.clear();
		}

		LoadClassTask loadClassTask = new LoadClassTask();
		loadClassTask.execute(0);
	}

	class LoadClassTask extends AsyncTask<Integer, Integer, Integer> {
		String msg = "";
		int code = AppConest.WS_SUCCESS;

		@Override
		protected Integer doInBackground(Integer... params) {
			CourseDao courseDao = new CourseDao(mySelf);
			sqldatas = courseDao.findAllCourse();
			if (sqldatas != null && sqldatas.size() != 0) {
				msg = "获取成功";
				code = AppConest.WS_SUCCESS;
			} else {
				msg = "获取失败";
				code = AppConest.WS_FALL;
			}
			return code;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);

			if (code == AppConest.WS_SUCCESS) {
				courseDatas.addAll(sqldatas);
				courseAdapter.notifyDataSetChanged();
			}
		}
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.mySelf = activity;
	}

	@Override
	public void onPause() {
		super.onPause(); // 停止翻页
		cb_couser_fragment_banner.stopTurning();
	}

}
